-- Lua Battle System - 示例战斗场景

-- 引入模块
local LuaBattleSystem = require("main")
local Character = require("characters.character")
local Enemy = require("characters.enemy")
local Player = require("characters.player")
local Skill = require("skills.skill")
local TacticalAI = require("ai.tactical_ai")
local Logger = require("utils.logger")

-- 示例战斗场景
local function example_battle()
    Logger.info("=== Lua Battle System 示例战斗场景 ===")
    Logger.info("版本: " .. LuaBattleSystem.version)
    Logger.info("===================================")
    
    -- 1. 创建角色
    Logger.info("\n1. 创建角色...")
    
    -- 创建玩家角色
    local player1 = Player:new({
        id = "player1",
        name = "勇者",
        level = 5,
        max_hp = 150,
        hp = 150,
        max_mp = 80,
        mp = 80,
        attack = 35,
        defense = 25,
        magic_attack = 20,
        magic_defense = 15,
        speed = 20,
        luck = 15,
        team_id = 1,
        position = 1,
        is_controlled = false
    })
    
    local player2 = Player:new({
        id = "player2",
        name = "法师",
        level = 4,
        max_hp = 120,
        hp = 120,
        max_mp = 100,
        mp = 100,
        attack = 15,
        defense = 10,
        magic_attack = 45,
        magic_defense = 25,
        speed = 18,
        luck = 12,
        team_id = 1,
        position = 2,
        is_controlled = false
    })
    
    -- 创建敌人角色
    local enemy1 = Enemy:new({
        id = "enemy1",
        name = "哥布林",
        level = 3,
        max_hp = 80,
        hp = 80,
        max_mp = 30,
        mp = 30,
        attack = 25,
        defense = 15,
        magic_attack = 10,
        magic_defense = 8,
        speed = 12,
        luck = 8,
        team_id = 2,
        position = 1,
        is_controlled = false
    })
    
    local enemy2 = Enemy:new({
        id = "enemy2",
        name = "骷髅战士",
        level = 4,
        max_hp = 100,
        hp = 100,
        max_mp = 40,
        mp = 40,
        attack = 30,
        defense = 20,
        magic_attack = 15,
        magic_defense = 12,
        speed = 10,
        luck = 10,
        team_id = 2,
        position = 2,
        is_controlled = false
    })
    
    Logger.info("创建角色完成:")
    Logger.info("- 玩家队伍: " .. player1.name .. "、" .. player2.name)
    Logger.info("- 敌人队伍: " .. enemy1.name .. "、" .. enemy2.name)
    
    -- 2. 为角色添加技能
    Logger.info("\n2. 为角色添加技能...")
    
    -- 创建基础技能
    local basic_attack = Skill:new({
        id = "basic_attack",
        name = "普通攻击",
        description = "对单个目标造成物理伤害",
        type = "physical",
        range = "single",
        target_type = "enemy",
        cost = {
            mp = 0,
            cooldown = 0
        },
        damage = {
            base = 20,
            multiplier = 1.0
        }
    })
    
    local fireball = Skill:new({
        id = "fireball",
        name = "火球术",
        description = "对单个目标造成魔法伤害",
        type = "magic",
        range = "single",
        target_type = "enemy",
        cost = {
            mp = 20,
            cooldown = 2
        },
        damage = {
            base = 30,
            multiplier = 1.5
        }
    })
    
    local heal = Skill:new({
        id = "heal",
        name = "治疗术",
        description = "恢复单个目标的生命值",
        type = "support",
        range = "single",
        target_type = "ally",
        cost = {
            mp = 15,
            cooldown = 2
        },
        healing = {
            base = 25,
            multiplier = 1.2
        }
    })
    
    -- 注册技能到技能管理器
    local SkillManager = require("skills.skill_manager")
    SkillManager:register_skill(basic_attack)
    SkillManager:register_skill(fireball)
    SkillManager:register_skill(heal)

    -- 为角色添加技能
    player1:add_skill(basic_attack)
    player1:add_skill(heal)

    player2:add_skill(basic_attack)
    player2:add_skill(fireball)

    enemy1:add_skill(basic_attack)
    enemy2:add_skill(basic_attack)
    
    Logger.info("技能添加完成:")
    Logger.info("- " .. player1.name .. ": " .. basic_attack.name .. "、" .. heal.name)
    Logger.info("- " .. player2.name .. ": " .. basic_attack.name .. "、" .. fireball.name)
    Logger.info("- " .. enemy1.name .. ": " .. basic_attack.name)
    Logger.info("- " .. enemy2.name .. ": " .. basic_attack.name)
    
    -- 3. 配置AI
    Logger.info("\n3. 配置AI...")
    
    -- 为敌人配置战术AI，启用决策树
    local ai1 = TacticalAI:new({
        use_decision_tree = true,
        strategy = "balanced"
    })
    
    local ai2 = TacticalAI:new({
        use_decision_tree = true,
        strategy = "aggressive"
    })
    
    enemy1.ai = ai1
    enemy2.ai = ai2
    
    Logger.info("AI配置完成:")
    Logger.info("- " .. enemy1.name .. ": 平衡策略，启用决策树")
    Logger.info("- " .. enemy2.name .. ": 激进策略，启用决策树")
    
    -- 4. 组建队伍
    Logger.info("\n4. 组建队伍...")
    
    local team1 = { player1, player2 } -- 玩家队伍
    local team2 = { enemy1, enemy2 } -- 敌人队伍
    
    Logger.info("队伍组建完成:")
    Logger.info("- 队伍1 (玩家): " .. #team1 .. " 名成员")
    Logger.info("- 队伍2 (敌人): " .. #team2 .. " 名成员")
    
    -- 5. 开始战斗
    Logger.info("\n5. 开始战斗...")
    Logger.info("战斗开始: " .. player1.name .. "、" .. player2.name .. " vs " .. enemy1.name .. "、" .. enemy2.name)
    
    local battle_id = LuaBattleSystem:start_battle(team1, team2)
    if not battle_id then
        Logger.error("战斗开始失败")
        return
    end
    
    Logger.info("战斗ID: " .. battle_id)
    Logger.info("战斗状态: 进行中")
    
    -- 6. 运行战斗回合
    Logger.info("\n6. 运行战斗回合...")
    
    local max_turns = 20 -- 最大回合数，防止无限循环
    local current_turn = 0
    local battle_state = "in_progress"
    
    while battle_state == "in_progress" and current_turn < max_turns do
        current_turn = current_turn + 1
        Logger.info("\n--- 第 " .. current_turn .. " 回合 ---")
        
        local result = LuaBattleSystem:run_turn(battle_id)
        if not result then
            Logger.error("运行回合失败")
            break
        end
        
        -- 输出回合结果
        Logger.info("当前角色: " .. result.current_character.name)
        if result.decision then
            Logger.info("决策: " .. (result.decision.type == "skill" and "使用技能: " .. result.decision.skill.name or "防御"))
            if result.decision.target then
                Logger.info("目标: " .. result.decision.target.name)
            end
        end
        
        if result.result then
            if result.result.damage then
                Logger.info("伤害: " .. result.result.damage)
            end
            if result.result.healing then
                Logger.info("治疗: " .. result.result.healing)
            end
        end
        
        -- 检查战斗状态
        local current_battle_state = LuaBattleSystem:get_battle_state(battle_id)
        if current_battle_state then
            battle_state = current_battle_state.battle_state
        else
            battle_state = "ended"
        end
        
        Logger.info("战斗状态: " .. battle_state)
    end
    
    -- 7. 战斗结果
    Logger.info("\n7. 战斗结果...")
    
    if current_turn >= max_turns then
        Logger.info("战斗结束: 达到最大回合数")
    else
        local final_state = LuaBattleSystem:get_battle_state(battle_id)
        if final_state then
            Logger.info("战斗结束: " .. final_state.result)
            Logger.info("获胜队伍: " .. final_state.winner_team_id)
        else
            Logger.info("战斗结束")
        end
    end
    
    -- 8. 战斗统计
    Logger.info("\n8. 战斗统计...")

    -- 获取当前战斗状态（在战斗结束前）
    local current_battle_id = battle_id
    if current_battle_id and LuaBattleSystem.battle_manager then
        local current_state = LuaBattleSystem:get_battle_state(current_battle_id)
        if current_state then
            Logger.info("战斗ID: " .. current_battle_id)
            Logger.info("总回合数: " .. (current_state.current_turn or 0))
            Logger.info("总伤害: " .. (current_state.total_damage_dealt or 0))
            Logger.info("总治疗: " .. (current_state.total_healing_done or 0))
            Logger.info("总技能使用次数: " .. (current_state.total_skills_used or 0))
            Logger.info("总状态效果: " .. (current_state.total_status_effects_applied or 0))
        else
            Logger.info("战斗已结束，无法获取最终统计")
        end
    end
    
    Logger.info("\n=== 示例战斗场景结束 ===")
    Logger.info("===================================")
end

-- 运行示例战斗
if arg and arg[1] == "example" then
    example_battle()
end

-- 导出函数
return {
    run_example = example_battle
}